Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 20/30 Β· 107.0 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Control structures

Compound statements

A pair of curly brackets { } and an enclosed sequence of statements
constitute a compound statement, which can be used wherever a statement
can be used.

If ... else

if (expr) {
//statements;
} else if (expr2) {
//statements;
} else {
//statements;
}

Conditional (ternary) operator

The conditional operator creates an expression that evaluates as one of
two expressions depending on a condition. This is similar to the if
statement that selects one of two statements to execute depending on a
condition. I.e., the conditional operator is to expressions what if is
to statements.

const result = condition ? expression : alternative;

is the same as:

if (condition) {
const result = expression;
} else {
const result = alternative;
}

Unlike the if statement, the conditional operator cannot omit its
"else-branch".

Switch statement

The syntax of the JavaScript switch statement is as follows:

switch (expr) {
case SOMEVALUE:
// statements;
break;
case ANOTHERVALUE:
// statements for when ANOTHERVALUE || ORNAOTHERONE
// no break statement, falling through to the following case
case ORANOTHERONE:
// statements specific to ORANOTHERONE (i.e. !ANOTHERVALUE &&
ORANOTHER);
break; //The buck stops here.
case YETANOTHER:
// statements;
break;
default:
// statements;
break;
}

β€’ break; is optional; however, it is usually needed, since otherwise
code execution will continue to the body of the next case block. This
fall through behavior can be used when the same set of statements apply
in several cases, effectively creating a disjunction between those
cases.
β€’ Add a break statement to the end of the last case as a precautionary
measure, in case additional cases are added later.
β€’ String literal values can also be used for the case values.
β€’ Expressions can be used instead of values.
β€’ The default case (optional) is executed when the expression does not
match any other specified cases.
β€’ Braces are required.

For loop

The syntax of the JavaScript for loop is as follows:

for (initial; condition; loop statement) {
/*
statements will be executed every time
the for{} loop cycles, while the
condition is satisfied
*/
}

or

for (initial; condition; loop statement(iteration)) // one statement

For ... in loop

The syntax of the JavaScript for ... in loop is as follows:

for (var property_name in some_object) {
// statements using some_object[property_name];
}

β€’ Iterates through all enumerable properties of an object.
β€’ Iterates through all used indices of array including all user-defined
properties of array object, if any. Thus it may be better to use a
traditional for loop with a numeric index when iterating over arrays.
β€’ There are differences between the various Web browsers with regard to
which properties will be reflected with the for...in loop statement. In
theory, this is controlled by an internal state property defined by the

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────